package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.BasePageInfo;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.service.admin.MarketFootprintService;
import com.cskaoyan.service.admin.MarketSearchHistoryService;
import com.cskaoyan.service.wx.WXGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Can
 * @since 2022/06/28 20:28
 */
@RestController
@RequestMapping("wx/goods")
public class WXGoodsController {
    @Autowired
    WXGoodsService wxGoodsService;
    @Autowired
    MarketSearchHistoryService searchHistoryService;
    @Autowired
    MarketFootprintService footprintService;

    /**
     * 获取商品数量
     *
     * @return com.cskaoyan.bean.vo.BaseRespVo
     * @author Can
     * @since 2022/06/28 20:29
     */
    @RequestMapping("count")
    public BaseRespVo countGoods() {
        BaseRespVo baseRespVo = wxGoodsService.countGoods();
        return baseRespVo;
    }

    /**
     * 获取一级类别 及其 所有的二级类别
     * 请求参数 id 这个id 有可能是一级id 有可能是二级id
     *
     * @param id
     * @return com.cskaoyan.bean.vo.BaseRespVo
     * @author Can
     * @since 2022/06/28 20:58
     */
    @RequestMapping("category")
    public BaseRespVo getCategories(Integer id) {
        BaseRespVo baseRespVo = wxGoodsService.getCategories(id);
        return baseRespVo;
    }

    /**
     * 获取商品列表
     * 可能通过类别id去获取 也可能通过品牌id获取
     * LC_R-SH
     * 通过keyword查找商品名获取 同时需更新关键词列表及搜索历史
     *
     * @param categoryId,brandId,keyword,info
     * @return com.cskaoyan.bean.BaseRespVo
     * @author Can
     * @since 2022/06/28 21:53
     */
    @RequestMapping("list")
    public BaseRespVo getGoodsList(Integer categoryId, Integer brandId, String keyword, BasePageInfo info, Boolean isNew, Boolean isHot) {
        BaseRespVo baseRespVo = null;
        if (categoryId != null) {
            baseRespVo = wxGoodsService.getGoodsListByCategoryId(categoryId, info.getPage(), info.getLimit());
        }
        if (brandId != null) {
            baseRespVo = wxGoodsService.getGoodsListByBrandId(brandId, info.getPage(), info.getLimit());
        }
        if (!StringUtils.isEmpty(keyword) && (info != null)) {
            baseRespVo = searchHistoryService.list(keyword, info);
        }
        if (isNew != null || isHot != null) {
            baseRespVo = wxGoodsService.getNewOrHotGoodsList(categoryId, info, isHot, isNew);
        }
        return baseRespVo;
    }

    /**
     * 获取商品推荐列表
     * 未完成  需要使用shiro subject对象获取数据
     *
     * @param id
     * @return
     * @author Can
     * @since 2022/06/28 22:25
     */
    @RequestMapping("related")
    public BaseRespVo getRelatedGoodsList(Integer id) {
        BaseRespVo baseRespVo = wxGoodsService.getRelatedGoodsList(id);
        return baseRespVo;
    }

    /**
     * 获取商品详情
     * 请求参数 商品id
     *
     * @param id
     * @return com.cskaoyan.bean.vo.BaseRespVo
     * @author Can
     * @since 2022/06/29 11:16
     */
    @RequestMapping("detail")
    public BaseRespVo getGoodsDetail(Integer id) {
        BaseRespVo baseRespVo = wxGoodsService.getGoodsDetail(id);

        // LC_R-SH
        // 点击查看商品时增加历史足迹
        footprintService.insert(id);

        return baseRespVo;
    }
}
